
const express = require('express')


//创建一个路由对象 
const router = express.Router()

//导出路由对象
module.exports = router;

router.use(
    express.json(),
    express.urlencoded({extended:true}), 
)

const db = require('../db');
const {formatData} = require('../utils')




    // /api/user/check
router.get('/check',(req,res)=>{
    console.log(777);
    const {username} = req.query;
    let sql = `select (username) from user where username='${username}'`
    db.query(sql,(result)=>{
        if(result.length>0){
            // res.send(formatData.success())
            res.send(formatData.success(result[0]))
        }else{
          
            // res.send(formatData({code:400}))
            res.send(formatData.fail())

        }
    })

})


//注册
    // /api/user/reg
// router.post('/reg',(req,res)=>{
//     console.log(6666);
//     const { username, password,email } = req.body;
//     let sql = `insert into user(username,password,email) values('${username}','${password}','${email}')`
//     db.query(sql,(result)=>{
//         if(result.insertId){
//             res.send(formatData.success())
//         }else{
          
//             res.send(formatData({code:400}))
//         }
//     })

// })

// //@登录    /api/user/login
// router.get('/login',(req,res)=>{
//     const {username,password} = req.query;
//     let sql = `select * from user where username='${username}' and password='${password}'`
//     db.query(sql,(result)=>{
//         if(result.length>0){
//             res.send(formatData.success(result[0]))
//         }else{
//             res.send(formatData.fail())
//         }
//     })
    
// })

